layui.use(['jquery','table','layer','laydate'],function () {
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var laydate = layui.laydate;

    var userIdd = localStorage.getItem("userId");
    var trueName = localStorage.getItem("trueName");
    var account = localStorage.getItem("account");
    var userName = localStorage.getItem("userName");

    laydate.render({
        elem:"#applyDate",
        type:"date",
        tigger:"click"
    })

    selectJsApplicant("","","");
    function selectJsApplicant(roleName,applyDate,result) {
        table.render({
            elem:"#root",
            url:"/JsApplicant/selectJsApplicant?roleName="+roleName+"&applyDate="+applyDate+"&result="+result,
            defaultToolbar: ['','',''],
            cellHeight:57,
            page:true,
            limit:10,
            limits:[10,15,20],
            size: 'lg',
            cols:[[
                {type:"checkbox",align:"center"},
                {type:"numbers",title:"序号",align:"center"},
                {field:"roleName",title:"创作者名称",align:"center"},
                {field:"position",title:"申请职务",align:"center"},
                {field:"applyDate",title:"申请时间",align:"center"},
                {field:"result",title:"申请状态",align:"center",templet:function (data) {
                        if(data.result=="1"){
                            return "待审核";
                        }else if(data.result=="2"){
                            return "已通过";
                        }else{
                            return "已驳回";
                        }
                    }},
                {title:"操作",align:"center",templet:function (data) {
                        if(data.result=="1"){
                            return "<div>"
                                +"<a href='javascript:chakan("+data.trId+");' style='color: #2F4056;margin-right: 10px;'>查看</a>"+
                                "<a href='javascript:shenhe(\""+data.trId+"\",\""+data.roleName+"\",\""+data.position+"\");' style='color: #2F4056;margin-right: 10px;'>审核</a>"
                                +"</div>";
                        }else{
                            return "<div>"
                                +"<a href='javascript:chakan("+data.trId+");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                +"</div>";
                        }
                    }}
            ]]
        })
    };

    //查看
    chakan = function chakan(trId) {
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"id":trId},
            url:"/JsApplicant/selectApplicantById",
            success:function (data) {
                chakanLayer(data.data);
            }
        });
    };
    function chakanLayer(data){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"查看创作者信息",
            content:"/user/user-applicant-see.html",
            area:['600px','600px'],
            btn:["关闭"],
            yes:function (index) {
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find('iframe').contents();
                obj.find("#roleName").val(data.roleName);
                obj.find("#roleDescirbe").val(data.roleDescirbe);
                obj.find("#position").val(data.position);
                if(data.trueName==""||data.trueName==null){
                    var trueName = "未知";
                }else{
                    var trueName = data.trueName;
                }
                obj.find("#trueName").val(trueName);
                if(data.result=="1"){
                    var result = "待审核";
                }else if(data.result=="2"){
                    var result =  "已通过";
                }else{
                    var result =  "已驳回";
                }
                obj.find("#result").val(result);
                obj.find("#implementDate").val(data.implementDate);
            }
        })
    };

    //审核
    shenhe = function shenhe(trId,roleName,position){
        var obj;
        layer.open({
            type:2,
            shode:0,
            content:"/user/user-applicant-audit.html",
            area:['500px','400px'],
            btn:["确定","取消"],
            yes:function (index) {
                var implementDate = new Date();
                implementDate = getTime(implementDate)
                var remarks = obj.find("#remarks").val();
                if(remarks==""){
                    layer.msg("请输入审核备注");
                }else{
                    var status = localStorage.getItem("status");
                    if(status==null){
                        status="2";
                    }
                        var json = {
                            implementId:userIdd,
                            remarks:remarks,
                            result:status,
                            id:trId,
                            implementDatee:implementDate
                        };
                    //console.log(json);
                        $.ajax({
                            type:"post",
                            dataType:"json",
                            data:json,
                            url:"/JsApplicant/aditApplicantStatus",
                            success:function (data) {
                                if(data){
                                    layer.msg("审核成功");
                                }else{
                                    layer.msg("审核失败");
                                }
                                layer.close(index);
                                location.reload();
                            }
                        });
                    };
            },btn2:function (index) {
              layer.close(index);
            },success:function (layero,index) {
                obj = layero.find('iframe').contents();
                obj.find('#trId').text(trId);
                obj.find("#roleName").text(roleName);
                obj.find("#position").text(position);
            }
        })
    }

    //搜素
    $("#sousuo").click(function () {
        var roleName = $("#roleName").val();
        var applyDate = $("#applyDate").val();
        selectJsApplicant(roleName,applyDate,"");
    })
    //重置
    $("#reset").click(function () {
        $("#roleName").val("");
        $("#applyDate").val("");
        selectJsApplicant("","","");
    });
    //展示
    show("quanbu");
    end("weishenhe");
    end("yitonggguo");
    end("yibohui");
    //按钮中的数量
    selectNumber("","","","#span1");
    selectNumber("","",1,"#span2");
    selectNumber("","",2,"#span3");
    selectNumber("","",3,"#span4");
    //全部审核
    $("#quanbu").click(function () {
        show("quanbu");
        end("weishenhe");
        end("yitonggguo");
        end("yibohui");
        selectJsApplicant("","","");
    });
    //未审核
    $("#weishenhe").click(function () {
        end("quanbu");
        show("weishenhe");
        end("yitonggguo");
        end("yibohui");
        selectJsApplicant("","",1);
    });
    //已审核
    $("#yitonggguo").click(function () {
        end("quanbu");
        end("weishenhe")
        show("yitonggguo");
        end("yibohui");
        selectJsApplicant("","",2);
    });
    //已驳回
    $("#yibohui").click(function () {
        end("quanbu");
        end("yitonggguo");
        end("weishenhe")
        show("yibohui");
        selectJsApplicant("","",3);
    });
    //显示
    function show(elementId) {
        var obj1 = document.getElementById(elementId);
        obj1.style.backgroundColor="#2F4056";
        obj1.style.color="#FFFFFF";
    };
    //不显示
    function end(elementId) {
        var obj2 = document.getElementById(elementId);
        obj2.style.backgroundColor="#F9F9F9";
        obj2.style.color="#666666";
        obj2.style.border="1px solid #D2D2D2";
    };
    //计算数量
    function selectNumber(roleName,applyDate,result,name) {
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"roleName":roleName,"applyDate":applyDate,"result":result},
            url:"/JsApplicant/countJsApplicant",
            success:function (data) {
                $(name).text(data);
            }
        })
    };

    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };
    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };

    //刷新
    $('#shuaxin').click(function () {
        location.reload();
    });

    //返回
    $("#goBack").click(function (s) {
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    });
})